
/* validate user */
var userForm = new Hash({
    login:0,
    email:0,
    password:0
});
function send_error(element, msg) {
    e = $(element);
    e.setStyle({
        border: '1px solid #FFCFCF',
        backgroundColor: '#FFEFEF',
        color: '#FF1A1A',
        margin: '0px',
        padding: '2px 5px'
        });
    e.innerHTML = msg;
    e.show();
}
function send_success(element, msg) {
    e = $(element);
    e.setStyle({
        border: '1px solid #E0F3FF',
        backgroundColor: '#EFF8FF',
        color: '#0197FB',
        margin: '0px',
        padding: '2px 5px'
        });    
    e.innerHTML = msg;
    e.show();
}

function check_user_login() {
    login = $F('user_login').strip();
    if (login == '') {
        send_error($('user_login_msg'), '用户名不能为空');
        return false;
    }
    if (login.match(/\d\w*/)) {
        send_error($('user_login_msg'), '用户名不能以数字开头');
        return false;
    }
    if (login.length < 4) {
        send_error($('user_login_msg'), '用户名长度不能小于3位');
        return false;
    }
    new Ajax.Request('/user/exist', {
        parameters :{login: login},
        onSuccess: function(transport) {
            result = transport.responseText;
            if (result == 'true')
                send_error($('user_login_msg'), '用户名已存在');
            else {
                send_success($('user_login_msg'), '用户名可以用');
                userForm['login'] = 1;
            }
        }
    });
}
function user_login_msg_clear() {
    $('user_login_msg').hide();
    $('submit_msg').hide();
    userForm['login'] = 0;
}

function check_user_email() {
    email = $F('user_email').strip();
    if (email.match(/\w+[\w.]*@[\w.]+\.\w+/)) {
        send_success($('user_email_msg'), 'Email正确');
        userForm['email'] = 1;
    } else {
        send_error($('user_email_msg'), 'Email格式不正确');
    }
}

function user_email_msg_clear() {
    $('user_email_msg').hide();
    $('submit_msg').hide();
    userForm['email'] = 0;
}

function check_user_pwd() {
    pwd = $F('user_password');
    if (pwd.length < 6) {
        send_error($('user_pwd_msg'), '密码不能小于六位');
    } else {
        send_success($('user_pwd_msg'), '格式正确');
    }
}
function user_pwd_msg_clear() {
    $('user_pwd_msg').hide();
    $('submit_msg').hide();
    userForm['password'] = 0;
}
function check_user_pwdc() {
    pwd = $F('user_password');
    pwdc = $F('user_password_confirmation');
    if (pwd.length < 6) {
        send_error($('user_pwd_msg'), '密码不能小于六位');
        return false;
    }
    if (pwd != pwdc) {
        send_error($('user_pwdc_msg'), '两次输入密码不一致');
        return false;
    } else {
        send_success($('user_pwdc_msg'), '格式正确');
        userForm['password'] = 1;
    }
}
function user_pwdc_msg_clear() {
    $('user_pwdc_msg').hide();
    $('submit_msg').hide();
    userForm['password'] = 0;
}

function submit_form(form) {
    var valid = true;
    userForm.each(function(p) {
        if (p.value == 0) valid = false;
    });
    if (!valid) {
        send_error($('submit_msg'), '请检查注册数据');
        return false;
    } else {
        form.submit();
    }
}

